package com.yex.controller;

import com.yex.base.BaseController;
import com.yex.service.ApiService;
import com.yex.service.LocaleMessageSourceService;
import com.yex.utils.JsonResultVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author Little Liu
 * @version V1
 * @date Created in 2018/12/25 17:50
 * @description 描述
 */
@Controller
public class ApiController extends BaseController {

    @Autowired
    private ApiService apiService;
    @Autowired
    private LocaleMessageSourceService message;

    @ResponseBody
    @PostMapping(value = "/appApi", produces = "application/json;charset=UTF-8")
    public JsonResultVo appApi(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(required = false, defaultValue = "0") String action) throws Exception {

        Long startTime = System.currentTimeMillis();
        JsonResultVo ret = null;
        try {
            request.setCharacterEncoding("UTF-8");
            showParams(request);
            /*curLoginToken = request.getParameter(AppConstant.LoginToken);
            boolean isLogin = redisService.isAppLogin(curLoginToken);

            if(isLogin){
                Fuser fuser = this.realTimeDataService.getAppFuser(curLoginToken);
                redisService.addOtcSession(fuser.getfId(), fuser.getFloginNames());
            }
*/

            if (StringUtils.isBlank(action)) {
                ret = new JsonResultVo(message.getMessage("parameter_error"), -1);
            } else {
                switch (action) {
                    case "register":
                        ret = apiService.register(request);
                        break;
                    case "login":
                        ret = apiService.login(request);
                        break;
                    case "signOut":
                        ret = apiService.signOut(request);
                        break;
                    case "signOutSystem":
                        ret = apiService.signOutSystem(request);
                        break;
                    case "setMoneyPassword":
                        ret = apiService.setMoneyPassword(request);
                        break;
                    case "forgetPassword":
                        ret = apiService.forgetPassword(request);
                        break;
                    case "modifyPassword":
                        ret = apiService.modifyPassword(request);
                        break;
                    case "modifyMoneyPassword":
                        ret = apiService.modifyMoneyPassword(request);
                        break;
                    case "resetMoneyPassword":
                        ret = apiService.resetMoneyPassword(request);
                        break;
                    case "userIntroList":
                        ret = apiService.userIntroList(request);
                        break;
                    case "userInfo":
                        ret = apiService.userInfo(request);
                        break;
                    case "uploadUserPhoto":
                        ret = apiService.uploadUserPhoto(request);
                        break;
                    case "realNameAuth":
                        ret = apiService.realNameAuth(request);
                        break;
                    case "addBank":
                        ret = apiService.addBank(request);
                        break;
                    case "bankList":
                        ret = apiService.bankList(request);
                        break;
                    case "deleteBank":
                        ret = apiService.deleteBank(request);
                        break;
                    case "userBank":
                        ret = apiService.userBank(request);
                        break;
                    case "orderConfigure":
                        ret = apiService.orderConfigure(request);
                        break;
                    case "addOrder":
                        ret = apiService.addOrder(request);
                        break;
                    case "payOrder":
                        ret = apiService.payOrder(request);
                        break;
                    case "revokeOrder":
                        ret = apiService.revokeOrder(request);
                        break;
                    case "orderList":
                        ret = apiService.orderList(request);
                        break;
                    case "orderInfo":
                        ret = apiService.orderInfo(request);
                        break;
                    case "transferWallet":
                        ret = apiService.transferWallet(request);
                        break;
                    case "buyInvest":
                        ret = apiService.buyInvest(request);
                        break;
                    case "transferInvest":
                        ret = apiService.transferInvest(request);
                        break;
                    case "sendSmg":
                        ret = apiService.sendSmg(request);
                        break;
                    case "verifySmg":
                        ret = apiService.verifySmg(request);
                        break;
                    case "question":
                        ret = apiService.question(request);
                        break;
                    case "questionList":
                        ret = apiService.questionList(request);
                        break;
                    case "noticeList":
                        ret = apiService.noticeList(request);
                        break;
                    case "readNotice":
                        ret = apiService.readNotice(request);
                        break;
                    case "investment":
                        ret = apiService.investment(request);
                        break;
                    case "userInvestmentOrder":
                        ret = apiService.userInvestmentOrder(request);
                        break;
                    case "userInvestment":
                        ret = apiService.userInvestment(request);
                        break;
                    case "investmentOrderList":
                        ret = apiService.investmentOrderList(request);
                        break;
                    case "addInvestmentOrder":
                        ret = apiService.addInvestmentOrder(request);
                        break;
                    case "replyInvestmentOrder":
                        ret = apiService.replyInvestmentOrder(request);
                        break;
                    case "redeemInvestmentOrder":
                        ret = apiService.redeemInvestmentOrder(request);
                        break;
                    case "consumptionRecord":
                        ret = apiService.consumptionRecord(request);
                        break;
                    case "teamManage":
                        ret = apiService.teamManage(request);
                        break;
                    default:
                        ret = new JsonResultVo(message.getMessage("parameter_error"), -1);
                        break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ret = new JsonResultVo(e.getMessage() == null ? message.getMessage("parameter_error") : e.getMessage(), -1);
        }
        System.out.println("---api time: " + (System.currentTimeMillis() - startTime) + "ms  ---action:----" + action);
        return ret;
    }

    private void showParams(HttpServletRequest request) {
        Map map = new HashMap();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }
            }
        }
        Set<Map.Entry> set = map.entrySet();
        System.out.println("------------------------------");
        for (Map.Entry entry : set) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println("------------------------------");
    }
}
